import { reactive, ref } from "vue";
import { getListApi } from "@/api/managementCenter/roleAndPermission";
import { getDeptTreeApi } from "@/api/managementCenter/departmentPersonnel";
import { getListApi as getAllWarehouseApi } from "@/api/managementCenter/warehosueManagement";
export default function InitHook() {
  const formState = reactive<Partial<DepartmentPersonnel.AddUserForm>>({
    name: undefined,
    username: undefined,
    password: "123456",
    status: undefined,
    phone: undefined,
    deptId: [],
    roleId: undefined,
    positionCode: undefined,
    warehouseList: [],
    weComId: "" //企业微信
  });
  const formDataRef = ref<any>(null);
  const loading = ref<boolean>(false);
  const roleList = ref<Array<Base.GeneraDataType>>([]);
  const departmentOpts = ref<Array<Base.GeneraDataType>>([]);
  const warehouseOpts = ref<Array<Base.GeneraDataType>>([]);
  const filter = (inputValue: string, path: Array<Base.GeneraDataType>) => {
    return path.some(option => option.name.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
  };
  const getAllRole = () => {
    return getListApi({ name: "", status: 1, pageNum: 1, pageSize: 10000, total: 0 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        roleList.value = data.records || [];
      }
    });
  };
  const getDeptTree = () => {
    return getDeptTreeApi().then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        departmentOpts.value = data || [];
      }
    });
  };
  const getAllWarehouseCode = () => {
    return getAllWarehouseApi({ pageNum: 1, pageSize: 99999, status: 1 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        warehouseOpts.value = data || [];
      }
    });
  };
  return {
    formState,
    formDataRef,
    departmentOpts,
    filter,
    roleList,
    getAllRole,
    getDeptTree,
    loading,
    warehouseOpts,
    getAllWarehouseCode
  };
}
